﻿using Microsoft.Extensions.Logging;
using PHMEE.Entities.DaqInfo.TdDevices;
using PHMEE.IRepositories.DaqInfo.TdDevices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Guids;
using Volo.Abp.Uow;

namespace PHMEE
{
    /// <summary>
    /// 生成 AMS 的TD种子数据
    /// </summary>
    public class AmsDataSeedManager : ITdDataSeedManager
    {

        public DbPrefixEnum DatabaseType => DbPrefixEnum.Ams;

        private readonly ILogger<AmsDataSeedManager> _logger;
        private readonly IGuidGenerator _guidGenerator;
        private readonly ITdDataBaseRepository _tdDatabaseRepository;
        private readonly ITdDeviceTypeRepository _tdDeviceTypeRepository;
        private readonly ITdDeviceAttributeRepository _tdDeviceAttributeRepository;
        private readonly ITdDeviceInstanceRepository _tdDeviceInstanceRepository;

        public AmsDataSeedManager(
            ILogger<AmsDataSeedManager> logger,
            IGuidGenerator guidGenerator,
            ITdDataBaseRepository tdDatabaseRepository,
            ITdDeviceTypeRepository tdDeviceTypeRepository,
            ITdDeviceAttributeRepository tdDeviceAttributeRepository,
            ITdDeviceInstanceRepository tdDeviceInstanceRepository)
        {
            _logger = logger;
            _guidGenerator = guidGenerator;
            _tdDatabaseRepository = tdDatabaseRepository;
            _tdDeviceTypeRepository = tdDeviceTypeRepository;
            _tdDeviceAttributeRepository = tdDeviceAttributeRepository;
            _tdDeviceInstanceRepository = tdDeviceInstanceRepository;
        }
        [UnitOfWork]
        public async Task<bool> SeedDataAsync(Guid vesselId, string dbDescription)
        {
            try
            {
                //数据库
                string dbName = PhmeeConst.GetDbName(DatabaseType, vesselId);

                var databaseId = _guidGenerator.Create();
                var database = new TdDatabase(databaseId)
                {
                    Name = dbName,
                    Description = dbDescription
                };
                await _tdDatabaseRepository.InsertAsync(database, autoSave: true);

                // 创建超级表、设备属性、子表数据
                var superDeviceTypes = CreateSuperDeviceTypes(databaseId);
                await _tdDeviceTypeRepository.InsertManyAsync(superDeviceTypes);

                var attributes = CreateDeviceAttributes(superDeviceTypes);
                await _tdDeviceAttributeRepository.InsertManyAsync(attributes);

                var subDeviceInstances = CreateDeviceInstances(superDeviceTypes);
                await _tdDeviceInstanceRepository.InsertManyAsync(subDeviceInstances);

                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError($"生成种子数据错误: {ex.Message}");
                return false;
            }
        }
        // 创建超级表
        private List<TdDeviceType> CreateSuperDeviceTypes(Guid databaseId)
        {
            return new List<TdDeviceType>
            {
                new TdDeviceType(_guidGenerator.Create())
                {
                    Name = "analog",
                    Description = "模拟量设备数据",
                    DatabaseId = databaseId
                }
            };
        }

        // 创建设备属性
        private List<TdDeviceAttribute> CreateDeviceAttributes(List<TdDeviceType> superDeviceTypes)
        {
            var superAnalogTableId = superDeviceTypes.First(d => d.Name == "analog").Id;

            return new List<TdDeviceAttribute>
            {
                // 模拟量
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ts", Description = "时间戳", ColumnType = "TIMESTAMP", TdDeviceTypeId = superAnalogTableId, SortOrder = 0 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "v", Description = "模拟量数值", ColumnType = "FLOAT", TdDeviceTypeId = superAnalogTableId, SortOrder = 1 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "u", Description = "模拟量单位", ColumnType = "INT", TdDeviceTypeId = superAnalogTableId, SortOrder = 2 }
            };
        }

        // 创建设备实例
        private List<TdDeviceInstance> CreateDeviceInstances(List<TdDeviceType> superDeviceTypes)
        {
            var superAnalogTableId = superDeviceTypes.First(d => d.Name == "analog").Id;

            return new List<TdDeviceInstance>
            {
                #region 主机
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma08", Description = "主机1#气缸排气口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma09", Description = "主机2#气缸排气口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma10", Description = "主机3#气缸排气口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma11", Description = "主机4#气缸排气口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma12", Description = "主机5#气缸排气口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma15", Description = "主机6#气缸排气口温度", TdDeviceTypeId = superAnalogTableId },

                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma16", Description = "主机1#增压器排气进口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma17", Description = "主机1#增压器排气出口温度", TdDeviceTypeId = superAnalogTableId },

                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml01", Description = "主机1#增压器滑油进口压力", TdDeviceTypeId = superAnalogTableId },

                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml25", Description = "主机1#气缸活塞冷却油出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml26", Description = "主机2#气缸活塞冷却油出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml27", Description = "主机3#气缸活塞冷却油出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml28", Description = "主机4#气缸活塞冷却油出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml29", Description = "主机5#气缸活塞冷却油出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml30", Description = "主机6#气缸活塞冷却油出口温度", TdDeviceTypeId = superAnalogTableId },

                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_mw03", Description = "主机1#气缸淡水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_mw04", Description = "主机2#气缸淡水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_mw05", Description = "主机3#气缸淡水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_mw06", Description = "主机4#气缸淡水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_mw07", Description = "主机5#气缸淡水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_mw08", Description = "主机6#气缸淡水出口温度", TdDeviceTypeId = superAnalogTableId },
	            #endregion


                #region 主机涡轮增压器
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma16", Description = "主机1#增压器排气进口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ma17", Description = "主机1#增压器排气出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ml01", Description = "主机1#增压器滑油进口压力", TdDeviceTypeId = superAnalogTableId },
                #endregion


                #region 1#柴油发电机
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge115", Description = "1#柴油发电机冷却水进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge116", Description = "1#柴油发电机润滑油进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge117", Description = "1#柴油发电机燃油进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge119", Description = "1#柴油发电机冷却水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge120", Description = "1#柴油发电机润滑油进口温度", TdDeviceTypeId = superAnalogTableId },

                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge122", Description = "1#柴油发电机1#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge123", Description = "1#柴油发电机2#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge124", Description = "1#柴油发电机3#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge125", Description = "1#柴油发电机4#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge126", Description = "1#柴油发电机5#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge127", Description = "1#柴油发电机6#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                #endregion

                #region 2#柴油发电机
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge215", Description = "2#柴油发电机冷却水进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge216", Description = "2#柴油发电机润滑油进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge217", Description = "2#柴油发电机燃油进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge219", Description = "2#柴油发电机冷却水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge220", Description = "2#柴油发电机润滑油进口温度", TdDeviceTypeId = superAnalogTableId },

                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge222", Description = "2#柴油发电机1#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge223", Description = "2#柴油发电机2#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge224", Description = "2#柴油发电机3#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge225", Description = "2#柴油发电机4#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge226", Description = "2#柴油发电机5#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge227", Description = "2#柴油发电机6#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                #endregion

               #region 3#柴油发电机
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge315", Description = "3#柴油发电机冷却水进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge316", Description = "3#柴油发电机润滑油进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge317", Description = "3#柴油发电机燃油进口压力", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge319", Description = "3#柴油发电机冷却水出口温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge320", Description = "3#柴油发电机润滑油进口温度", TdDeviceTypeId = superAnalogTableId },

                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge322", Description = "3#柴油发电机1#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge323", Description = "3#柴油发电机2#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge324", Description = "3#柴油发电机3#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge325", Description = "3#柴油发电机4#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge326", Description = "3#柴油发电机5#气缸排气温度", TdDeviceTypeId = superAnalogTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "a_ge327", Description = "3#柴油发电机6#气缸排气温度", TdDeviceTypeId = superAnalogTableId }
                #endregion
            };
        }

    }
}
